{
 "cells": [
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-06-26T02:38:17.835545Z",
     "start_time": "2025-06-26T02:38:17.818868Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 读写文件\n",
    "import torch\n",
    "from torch import nn\n",
    "from torch.nn import functional as F\n",
    "\n",
    "x = torch.arange(4)\n",
    "torch.save(x, 'x-file')"
   ],
   "id": "b8bbe8f647082314",
   "outputs": [],
   "execution_count": 8
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-06-26T02:38:17.882249Z",
     "start_time": "2025-06-26T02:38:17.868454Z"
    }
   },
   "cell_type": "code",
   "source": [
    "x2 = torch.load('x-file')\n",
    "x2"
   ],
   "id": "7d3fbcd3888ecd0b",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([0, 1, 2, 3])"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 9
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-06-26T02:38:58.724600Z",
     "start_time": "2025-06-26T02:38:58.717860Z"
    }
   },
   "cell_type": "code",
   "source": [
    "y = torch.zeros(4)\n",
    "torch.save([x, y], 'x-file')\n",
    "x2, y2 = torch.load('x-file')\n",
    "(x2, y2)"
   ],
   "id": "850a2ab62a46703c",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(tensor([0, 1, 2, 3]), tensor([0., 0., 0., 0.]))"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 11
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-06-26T02:39:09.045913Z",
     "start_time": "2025-06-26T02:39:09.032345Z"
    }
   },
   "cell_type": "code",
   "source": [
    "mydict = {'x': x, 'y': y}\n",
    "torch.save(mydict, 'mydict')\n",
    "mydict2 = torch.load('mydict')\n",
    "mydict2"
   ],
   "id": "f58a401099f16ad6",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'x': tensor([0, 1, 2, 3]), 'y': tensor([0., 0., 0., 0.])}"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 12
  },
  {
   "metadata": {},
   "cell_type": "code",
   "outputs": [],
   "execution_count": null,
   "source": [
    "class MLP(nn.Module):\n",
    "    def __init__(self):\n",
    "        super().__init__()\n",
    "        self.hidden = nn.Linear(20, 256)\n",
    "        self.output = nn.Linear(256, 10)\n",
    "\n",
    "    def forward(self, x):\n",
    "        return self.output(F.relu(self.hidden(x)))\n",
    "\n",
    "net = MLP()\n",
    "X = torch.randn(size=(2, 20))\n",
    "Y = net(X)"
   ],
   "id": "35ba457d24c6876f"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-06-26T02:46:53.789115Z",
     "start_time": "2025-06-26T02:46:53.767438Z"
    }
   },
   "cell_type": "code",
   "source": [
    "torch.save(net.state_dict(), 'mlp.params')\n",
    "net.state_dict()"
   ],
   "id": "66b5f923198abd2c",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "OrderedDict([('hidden.weight',\n",
       "              tensor([[-0.1142, -0.0224, -0.1193,  ..., -0.0986, -0.0795, -0.1731],\n",
       "                      [ 0.1767, -0.1495, -0.0223,  ..., -0.1648,  0.1719,  0.2090],\n",
       "                      [-0.1322,  0.1602, -0.0078,  ..., -0.0198, -0.1949,  0.0832],\n",
       "                      ...,\n",
       "                      [ 0.1811, -0.1541,  0.0897,  ...,  0.1424,  0.0302, -0.1661],\n",
       "                      [-0.0681, -0.0129, -0.1759,  ..., -0.0302,  0.2061,  0.2225],\n",
       "                      [-0.2039, -0.1590,  0.2037,  ..., -0.2106,  0.1438, -0.0018]])),\n",
       "             ('hidden.bias',\n",
       "              tensor([ 0.0087, -0.0044, -0.1351,  0.1856,  0.0873, -0.0495, -0.1696,  0.1334,\n",
       "                      -0.1144,  0.1209,  0.1066,  0.0524,  0.0698,  0.1875,  0.0256,  0.1441,\n",
       "                       0.1404, -0.0774,  0.0962,  0.1789, -0.1751, -0.1432, -0.0228, -0.2090,\n",
       "                      -0.0240,  0.0290,  0.1804,  0.0705,  0.0025,  0.0204, -0.0396, -0.1229,\n",
       "                       0.1133, -0.0162,  0.0954,  0.1753, -0.0294,  0.0761,  0.0430, -0.1970,\n",
       "                      -0.1413, -0.0957,  0.0396, -0.1897,  0.0578, -0.1780,  0.0470,  0.1183,\n",
       "                       0.1984, -0.1585, -0.1221,  0.1099, -0.1627,  0.0913, -0.1245, -0.2053,\n",
       "                      -0.1714, -0.0757, -0.0953, -0.1279, -0.1346, -0.0558,  0.1213, -0.1484,\n",
       "                      -0.0911, -0.1884, -0.1679, -0.2120,  0.0507, -0.1660,  0.1198, -0.0239,\n",
       "                       0.2166,  0.1433, -0.0122,  0.1154, -0.1641, -0.1946, -0.1797, -0.1369,\n",
       "                      -0.1520,  0.1420,  0.0466, -0.1859, -0.1994, -0.0149,  0.2048,  0.1879,\n",
       "                      -0.1410,  0.1399,  0.1052, -0.1636,  0.1610, -0.0712,  0.0788,  0.2122,\n",
       "                      -0.0575,  0.0928, -0.1480, -0.2100,  0.2216, -0.0515, -0.2114,  0.0463,\n",
       "                       0.0573, -0.2064,  0.1625, -0.1504,  0.1718,  0.2221,  0.1261,  0.0768,\n",
       "                       0.0878, -0.1628, -0.1491,  0.0974, -0.0693, -0.1769,  0.1567,  0.1851,\n",
       "                      -0.2167, -0.1156,  0.0069,  0.1756, -0.0713,  0.0809,  0.0800,  0.0308,\n",
       "                      -0.0392, -0.0209,  0.0333,  0.0476, -0.1933,  0.0749,  0.2045, -0.0145,\n",
       "                      -0.1827,  0.1067,  0.0749,  0.0399, -0.1404, -0.0936,  0.0722,  0.1388,\n",
       "                       0.1197,  0.2140,  0.0990, -0.0912,  0.0389,  0.0082,  0.1516,  0.0786,\n",
       "                      -0.1703, -0.0805, -0.1470,  0.0995, -0.1024, -0.1891, -0.0222, -0.0093,\n",
       "                      -0.0095,  0.1910, -0.2039, -0.0597,  0.2000,  0.1885,  0.1684,  0.0061,\n",
       "                       0.0584, -0.2134, -0.0676, -0.1017,  0.2110, -0.0434,  0.1109,  0.0614,\n",
       "                       0.1861,  0.0532, -0.0237,  0.1736, -0.2076,  0.1854,  0.0462, -0.1779,\n",
       "                      -0.0252, -0.1561,  0.0784, -0.1908,  0.2088,  0.0806, -0.1492,  0.1462,\n",
       "                       0.1902, -0.0658,  0.1342,  0.0137,  0.0374,  0.1368, -0.1042,  0.0762,\n",
       "                       0.1338,  0.1745, -0.1966,  0.0074,  0.0267, -0.1639,  0.1836, -0.0142,\n",
       "                      -0.0178, -0.2000,  0.0375,  0.0519,  0.1488, -0.1094, -0.0697, -0.1518,\n",
       "                       0.0173,  0.1124, -0.0175,  0.0313,  0.0881, -0.0468,  0.0917, -0.1253,\n",
       "                      -0.0591, -0.0601, -0.1801, -0.0529, -0.1368,  0.1663, -0.0959,  0.0892,\n",
       "                       0.1329, -0.1808,  0.1065,  0.1263, -0.0163, -0.1703, -0.1219,  0.1884,\n",
       "                       0.0602,  0.0333, -0.0513, -0.2192, -0.0124,  0.1728, -0.1468,  0.1350,\n",
       "                      -0.1274,  0.1452,  0.0286, -0.1625,  0.1345,  0.0470, -0.1363, -0.1414])),\n",
       "             ('output.weight',\n",
       "              tensor([[-0.0530,  0.0213,  0.0100,  ..., -0.0173,  0.0196,  0.0269],\n",
       "                      [-0.0069, -0.0536,  0.0487,  ..., -0.0176,  0.0287, -0.0179],\n",
       "                      [-0.0531, -0.0094, -0.0342,  ...,  0.0584,  0.0403, -0.0453],\n",
       "                      ...,\n",
       "                      [-0.0458,  0.0396,  0.0196,  ...,  0.0414,  0.0533,  0.0475],\n",
       "                      [ 0.0392,  0.0425,  0.0250,  ...,  0.0594,  0.0603, -0.0556],\n",
       "                      [-0.0182, -0.0255,  0.0049,  ..., -0.0439,  0.0468,  0.0344]])),\n",
       "             ('output.bias',\n",
       "              tensor([ 0.0130,  0.0221,  0.0271,  0.0184,  0.0085, -0.0028,  0.0579, -0.0604,\n",
       "                       0.0541, -0.0329]))])"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 15
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-06-26T02:47:04.363912Z",
     "start_time": "2025-06-26T02:47:04.351532Z"
    }
   },
   "cell_type": "code",
   "source": [
    "clone = MLP()\n",
    "clone.load_state_dict(torch.load('mlp.params'))\n",
    "clone.eval()"
   ],
   "id": "87cb5aa4f1b7b046",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "MLP(\n",
       "  (hidden): Linear(in_features=20, out_features=256, bias=True)\n",
       "  (output): Linear(in_features=256, out_features=10, bias=True)\n",
       ")"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 16
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-06-26T02:47:13.322546Z",
     "start_time": "2025-06-26T02:47:13.310538Z"
    }
   },
   "cell_type": "code",
   "source": [
    "Y_clone = clone(X)\n",
    "Y_clone == Y"
   ],
   "id": "d81d54e7429c1337",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[True, True, True, True, True, True, True, True, True, True],\n",
       "        [True, True, True, True, True, True, True, True, True, True]])"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 17
  },
  {
   "metadata": {},
   "cell_type": "code",
   "outputs": [],
   "execution_count": null,
   "source": "",
   "id": "e2116ed50707089c"
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.22"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
